import { isBoolean, isEmpty, isFunction, isString } from "@/utils"
import { GetanTableColumnProps, GetanTableProps, GetanTableSorterType } from "../types"
import { useIndex } from "./useIndex"
import { hasPermission } from "@/components/Permission"
import { useRowActions } from "./useRowActions"

/**
 * 列配置
 */
export function useColumns(props: Ref<Partial<GetanTableProps>>, sorter: Ref<GetanTableSorterType | undefined>) {
    // 所有配置列
    const columnsRef = computed(() => unref(props).columns ?? [])

    const { indexColumn } = useIndex(props)

    const { rowActionColumn } = useRowActions(props)
    // 显示列
    const showColumns = computed(() => {
        const columns = columnsRef.value
            .filter(column => {
                const { show } = column
                if (isBoolean(show)) {
                    return show as boolean
                }
                if (isFunction(show)) {
                    return (show as Function)(column)
                }
                return true
            })
            .map(column => {
                const hasPerm = hasPermission(column)
                let naiveColumn = undefined
                if (hasPerm) {
                    naiveColumn = parseColumn(column)
                } else {
                    const { showWhenNoPermission } = column
                    if (showWhenNoPermission) {
                        if (isString(showWhenNoPermission)) {
                            const render = () => "****"
                            naiveColumn = parseColumn(column, render)
                        } else if (isFunction(showWhenNoPermission)) {
                            naiveColumn = parseColumn(column, showWhenNoPermission as Function)
                        }
                    }
                }
                return naiveColumn
            })
            .filter(column => column !== undefined) as Recordable[]
        if (!isEmpty(columns) && indexColumn.value) {
            columns.unshift(indexColumn.value)
        }
        if (!isEmpty(columns) && rowActionColumn.value) {
            columns.push(parseColumn(rowActionColumn.value as GetanTableColumnProps))
        }
        return columns
    })

    const parseColumn = (column: GetanTableColumnProps, renderFunction?: Function) => {
        const c: Recordable = {
            title: column.title,
            key: column.property,
            width: column.width,
            align: column.align,
            fixed: column.fixed,
            resizable: column.resizable ?? true
        }
        const { property, sorter: columnSorter, ellipsis, rowDisabled, render } = column
        if (columnSorter === true) {
            c.sorter = true
            c.sortOrder = sorter.value?.property === property ? sorter.value?.order : false
        } else if (isString(columnSorter)) {
            c.sorter = true
            c.sortOrder = sorter.value?.property === property ? sorter.value?.order : false
        }
        if (isBoolean(rowDisabled)) {
            c.disabled = () => rowDisabled
        } else if (isFunction(rowDisabled)) {
            c.disabled = rowDisabled
        }
        if (ellipsis) {
            c.ellipsis = { tooltip: true }
        }
        if (isFunction(render)) {
            c.render = render
        } else if (isFunction(renderFunction)) {
            c.render = renderFunction
        }
        return c
    }

    return { columns: columnsRef, showColumns }
}
